package com.attilax.io;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//import org.jfree.chart.ChartFactory;






import java.util.Map;
import java.util.function.Function;

import org.apache.commons.io.FileExistsException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

 
import com.attilax.core.ForeachFunctionV2;
// r626 import com.attilax.dataspider.TsaolyoNetDataSpider;
 
import com.attilax.text.strUtil;
import com.attilax.util.ExUtil;
import com.cnhis.cloudhealth.module.log.Cls1;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

 

/**
 * v s61   del empty dir ,,move
 * @author attilax
 *
 */
public class DirService {
	
	public static final Logger logger = LoggerFactory
			.getLogger(DirService.class);
	
	
	

	/**
	@author attilax 閿熸枻鎷烽敓妗旂鎷风埅閿熸枻鎷�
		@since  2014-5-27 閿熸枻鎷烽敓鏂ゆ嫹02:42:43$
	
	 * @param s
	 * @return
	 */
	public static String getParentPath(String s) {
		// attilax 閿熸枻鎷烽敓妗旂鎷风埅閿熸枻鎷� 閿熸枻鎷烽敓鏂ゆ嫹02:42:43   2014-5-27 
		File f=new File(s); 
		return f.getParent();
	}


	//	public static void traveDirPa1(String strPath,
//			com.attilax.lang.Closure closure) {
//		strPath=pathx.fixSlash(strPath);
//		String[] a=strPath.split(",");
//		for (String dir_t : a) {
//			if(dir_t.trim().length()==0)
//				continue;
//			if (dir_t.contains("闂嗐劍鐏�))
//				System.out.println("dbg");
//			File dir = new File(dir_t);
//			if(!dir.exists())
//				continue;
//			File[] files = dir.listFiles();
//			int length = files.length;
//
//			for (int i = 0; i < files.length; i++) {
//				if (files[i].isFile())
//					continue;
//				//dir is 
//				try {
//					closure.execute(files[i].getAbsolutePath());
//					traveDirPa1(files[i].getAbsolutePath(), closure);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//
//			}
//
//		}
//		
//	}
	/*for (int i = 0; i < length; i++) { 
	        if (files[i].isDirectory()) { 
	        	trave(files[i].getAbsolutePath(),closure); 
	        } else { 
	            String strFileName = files[i].getAbsolutePath().toLowerCase();
	            
	            try {
					closure.execute(files[i].getAbsolutePath());
				} catch (Exception e) {
					throw  new RuntimeException("",e);
				}  
	            
	        } 
	    } 
	 * */
	private static boolean isLastLevDir(File dir) {
		//   File dir = new File(strPath); 
		    File[] files = dir.listFiles(); 
			for (int i = 0; i < files.length; i++) { 
		        if (files[i].isDirectory()) { 
		        	return false;
		        }  
		    } 
		return true;
	}

//	public static List<String> getFiles(String strPath) {
//		travDir.refreshFileList(strPath);
//		List<String> li = travDir.filelist;
//		
//		return li;
//	}

	public static void main(String[] args) {
		 long a=System.currentTimeMillis();
		 tt();
		// YamlAtiX.getList(f)
		 
		// moveEmptyDir("C:\\codelibcaptch","C:\\codelibcaptch_emptydir");
		
		 long a2=System.currentTimeMillis();
		 System.out.println(a2-a);
		 System.out.println("--f");
		// 138.396 s
		 //if dif  dirs  .only need  20s
		 
	}

	public static void moveDir2dest(File f, String dest) throws IOException {
		
		
		try {
			FileUtils.moveDirectoryToDirectory(f, new File(dest),true);
		} catch (FileExistsException   e) {
			FileUtils.deleteDirectory(f);
		}
		
	}

	 
	/**
	 * ingo ex ,and log ,and save ex in to li..
	 * * 
	 * 
	 * 
	 * @param strPath
	 * @param fun
	 */

	public static void moveDir2dest(File f, String destDirRoot, String dirRoot) {
		   String rltFilename=FileUtilsAti.rltpath(f.getAbsolutePath(), dirRoot);
		String newname=destDirRoot+File.separator+rltFilename;
		try {
			FileUtils.moveDirectory(f, new File(newname));
		} catch(FileExistsException e)
		{
			try {
				FileUtils.deleteDirectory(f);
			} catch (IOException e1) {
				ExUtil.throwExV2(e1);
			}
		}catch (IOException   e) {
			 ExUtil.throwExV2(e);
		}
		
	}

	/**
		 * traveFile_noIncDir  only file 
		 * @param strPath
		 * @param closure
		 */
	 
		public static void traveV3(String strPath ,Function   closure) { 
		    File dir = new File(strPath); 
		    File[] files = dir.listFiles(); 
		    
		    if (files == null) 
		        return; 
		    int length = files.length;
			for (int i = 0; i < length; i++) { 
		        if (files[i].isDirectory()) { 
		        	traveV3(files[i].getAbsolutePath(),closure); 
		        } else { 
		            String strFileName = files[i].getAbsolutePath().toLowerCase();
		            
		            try {
						closure.apply(files[i].getAbsolutePath());
					} catch (Exception e) {
						throw  new RuntimeException("traveV3 ex,f:"+strFileName,e);
					}  
		            
		        } 
		    } 
		}

	private static void tt() {
//		String d=",z:/閻栬鲸鍎忕猾锟�z:/閸斻劎鏁剧猾锟�z:/閸斻劋缍旂猾锟�z:/濞擃垰褰撮崶鎴掗獓,z:/閸撗勫剰缁拷,z:/缁夋垵澶熺猾锟�z:/閹劖锟芥牜琚�z:/閹剛鏋掔猾锟�z:/閹存ü绨ょ猾锟�z:/閸犳粌澧界猾锟�;
//		DirService.traveDirPa1(d, new com.attilax.lang.Closure<String, String>() {
//
//			@Override
//			public String execute(String f) throws Exception {
//				if(f.contains("@Recycle") || f.contains("player"))
//						return null;
//				f = pathx.fixSlash(f);
//				System.out.println(f);
//				 
//				return null;
//			}
//		});
	}
	 
	 
	
int count;

 private String dirname;
	
	
	
	
		List<Map> err_li=Lists.newArrayList();
	public boolean traveCanBreakThrowEx=false;
 

	
	public String traveMode="all";  //dir,file   
	
 
	public DirService() {
		// TODO Auto-generated constructor stub
	}
	public DirService(String dirname) {
		this.dirname=dirname;
	}
	/**  
	 *  not safe ver
	@author attilax 閼颁礁鎼伴惃鍕焻鐎涳拷
	\t@since  Aug 2, 2014 3:40:56 AM$
	
	 * @param strPath
	 */
//	public static void trave_NS(String strPath ,Closure closure) { 
//	    File dir = new File(strPath); 
//	    File[] files = dir.listFiles(); 
//	    
//	    if (files == null) 
//	        return; 
//	    for (int i = 0; i < files.length; i++) { 
//	        if (files[i].isDirectory()) { 
//	        	trave(files[i].getAbsolutePath(),closure); 
//	        } else { 
//	            String strFileName = files[i].getAbsolutePath().toLowerCase();
//	            
//	            try {
//					closure.execute(files[i].getAbsolutePath());
//				} catch (Exception e) {
//					throw  new RuntimeException("",e);
//				}  
//	            
//	        } 
//	    } 
//	}

	public DirService setTraveMode(String traveMode2) {
		this. traveMode= traveMode2;
		return this;
	}
//
//	public void trave(String strPath, com.attilax.lang.Closure closure) {
//		 File dir = new File(strPath); 
//		    File[] files = dir.listFiles(); 
//		    
//		    if (files == null) 
//		        return; 
//		    int length = files.length;
//			for (int i = 0; i < length; i++) { 
//		        if (files[i].isDirectory()) { 
//		        	trave(files[i].getAbsolutePath(),closure); 
//		        } else { 
//		            String strFileName = files[i].getAbsolutePath().toLowerCase();
//		            
//		            try {
//						closure.execute(files[i].getAbsolutePath());
//					} catch (Exception e) {
//						throw  new RuntimeException("",e);
//					}  
//		            
//		        } 
//		    } 
//	}
	public List<String> subDirs() {
		 
		 File[] files = 	new File(this.dirname).listFiles(); 
		 List<String> li=new ArrayList<String>();
		 for (File f : files) {
			 li.add(f.getName());
		}
		return li;
	}
 

}
